package com.example.user.customviewdemo.drawing.xfermode;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.os.Build;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.example.user.customviewdemo.R;

/**
 * Created by user on 2016/5/5.
 * 利用PoertDuffXfermode中的Mode.SRC_OUT实现橡皮擦效果
 */
public class RubberView extends View {

    private float mPreX,mPreY;
    private Path path;
    private Paint paint;
    private Paint textPaint;

    private Bitmap srcBmp,dstBmp,textBmp;
    public RubberView(Context context) {
        super(context);
    }

    public RubberView(Context context, AttributeSet attrs) {
        super(context, attrs);
        path = new Path();
        paint = new Paint();
        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(30);
        paint.setAntiAlias(true);

        textPaint = new Paint();
        textPaint.setTextAlign(Paint.Align.CENTER);
        textPaint.setTextSize(50);
        textPaint.setColor(Color.BLUE);
        textPaint.setAntiAlias(true);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            textPaint.setLetterSpacing(0.5f);
        }

        srcBmp = BitmapFactory.decodeResource(getResources(), R.mipmap.yangjie);
        dstBmp = Bitmap.createBitmap(srcBmp.getWidth(),srcBmp.getHeight(), Bitmap.Config.ARGB_8888);
        textBmp = Bitmap.createBitmap(srcBmp.getWidth(),srcBmp.getHeight(), Bitmap.Config.ARGB_8888);
    }

    public RubberView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        /**
         * 先制造一张请叫我杨杰的bitmap，然后绘制出来
         */
        Canvas c = new Canvas(textBmp);
        c.drawText("请叫我杨杰",textBmp.getWidth()/2,textBmp.getHeight()/2,textPaint);
        canvas.drawBitmap(textBmp,0,0,paint);


        /**
         * 制造手势控制图设置为目标图层
         */
        Canvas c1 = new Canvas(dstBmp);
        c1.drawPath(path,paint);

        /**
         * 开始离屏绘制
         */
        int layerID = canvas.saveLayer(0,0,getWidth(),getHeight(),null,Canvas.ALL_SAVE_FLAG);
        canvas.drawBitmap(dstBmp,0,0,paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT));
        canvas.drawBitmap(srcBmp,0,0,paint);
        paint.setXfermode(null);
        canvas.restoreToCount(layerID);
    }



    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction();
        switch (action){
            case MotionEvent.ACTION_DOWN:
                path.moveTo(event.getX(),event.getY());
                mPreX  = event.getX();
                mPreY = event.getY();
                return true;
            case MotionEvent.ACTION_MOVE:
                float endX = (mPreX+event.getX())/2;
                float endY = (mPreY+event.getY())/2;
                path.quadTo(mPreX,mPreY,endX,endY);
                mPreX = event.getX();
                mPreY =event.getY();
                break;
            case MotionEvent.ACTION_UP:
                break;
        }
        postInvalidate();
        return super.onTouchEvent(event);
    }
}
